<template>
	<view class="container">
		<view>
			<u--form labelPosition="left" :model="form" :rules="rules" ref="form" labelWidth='100px'
				:labelStyle='labelStyle'>
				
				
				<view class="box">
					<u-form-item label="" prop="ssycx" >
						<view class="textarea">
							<text class="title">设施异常项</text>
							<view class="tarea">
								<textarea
									style="background-color: #F6F6F6;color: #666666;font-size: 28rpx;border-radius: 8rpx; padding: 24rpx 0 24rpx 10rpx;"
									v-model="form.ssycx" placeholder="请输入……" rows="3"></textarea>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="维修状态 " prop="wxztName" borderBottom @click="show2=true">
						<u--input v-model="form.wxztName" placeholder="请选择维修状态" disabledColor="#ffffff" disabled
							border="none"></u--input>
						<u-icon slot="right" name="arrow-right" ></u-icon>
					</u-form-item>
					<u-picker :show="show2" :columns="columns2" keyName="label" @confirm='confirm2'
						@cancel='show2=false'></u-picker>
					<u-form-item label="" prop="wxqk" >
						<view class="textarea">
							<text class="title">维修状况</text>
							<view class="tarea">
								<textarea
									style="background-color: #F6F6F6;color: #666666;font-size: 28rpx;border-radius: 8rpx; padding: 24rpx 0 24rpx 10rpx;"
									v-model="form.wxqk" placeholder="请输入……" rows="3"></textarea>
							</view>
						</view>
					</u-form-item>
					<user-pick :label="'维修负责人'" :multiple="false" :title="'维修负责人'" :placeholder="'请选择维修负责人'"
						v-model="form.wxfzr" :prop="'wxfzr'"></user-pick>
					<dept :label="'维修负责单位'" :title="'维修负责单位'" :placeholder="'请选择维修负责单位'" v-model="form.wxfzrdw" :prop="'wxfzrdw'"></dept>
					<u-form-item label="维修开始日期" prop="wxksrq" borderBottom  @click="show=true">
						<u--input v-model="form.wxksrq" placeholder="维修开始日期" disabledColor="#ffffff" disabled
							border="none"></u--input>
						<u-icon slot="right" name="arrow-right" ></u-icon>
					</u-form-item>
					<u-form-item label="维修结束日期" prop="wxjsrq" borderBottom  @click="show1=true">
						<u--input v-model="form.wxjsrq" placeholder="维修结束日期" disabledColor="#ffffff" disabled
							border="none"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
					<u-calendar :show="show" @close='show=false'  @confirm="confirm"></u-calendar>
					<u-calendar :show="show1" @close='show1=false'  @confirm="confirm1"></u-calendar>
					
					<u-form-item label="维修标准附件上传" borderBottom>
						<view class="imageBigBox" v-for="(item,i) in imageList" :key="i">
							<image style="width: 200rpx;height: 200rpx;" @click="previewImg(item)" :src="item"></image>
							<u-icon class="delIcon" name="close-circle" color="#999" size="25" @click="delectImg(i)">
							</u-icon>
						</view>
						<view class="uoloadImg-box" @click="chooseFile" v-if="imageList.length<1">
							<view class="jia"
								style="width: 200rpx;height: 200rpx;display: flex;justify-content: center;align-items: center;">
								<image style="width: 90rpx;height: 80rpx;" src="/static/image/photo.png" mode="">
								</image>
							</view>
						</view>
					</u-form-item>
					
				</view>
			</u--form>
			<u-button type="primary" text="确认" @click="submit" class='but'></u-button>
		</view>
	</view>
</template>

<script>
	import {
		wxadd,
	} from '@/api/equipment'
	import {
		URL
	} from '@/api/request.js'
	import userPick from "@/pages/Components/userPick";
	import dept from "@/pages/Components/department";
	export default {
		components: {
			userPick,
			dept
		},
		data() {
			return {
				show: false,
				show1:false,
				show2:false,
				labelStyle: {
					color: '#666'
				},
				imageList: [], //本地上传图片数组
				list: [], //上传后端后返回的图片链接数组
				form: {
					scssid:'',
					ssycx: '',
					wxqk: '',
					wxfzr: '',
					wxztName: '',
					wxfzrdw: '',
					wxjsrq: '',
					wxksrq: '',
					wxbzfj: ''
				},
				id: '',
				columns2: [
					[{
						label: '维修中',
						id: 1
					}, {
						label: '已修好',
						id: 2
					}]
				],
				rules: {
					'ssycx': {
						type: 'string',
						required: true,
						message: '不能为空',
						trigger: ['blur', 'change']
					},
					'wxqk': {
						type: 'string',
						required: true,
						message: '不能为空',
						trigger: ['blur', 'change']
					},
					'wxfzr': {
						type: 'string',
						required: true,
						message: '不能为空',
						trigger: ['blur', 'change']
					},
					'wxztName': {
						type: 'string',
						required: true,
						message: '不能为空',
						trigger: ['blur', 'change']
					},
					'wxfzrdw': {
						type: 'string',
						required: true,
						message: '不能为空',
						trigger: ['blur', 'change']
					},
					'wxjsrq': {
						type: 'string',
						required: true,
						message: '不能为空',
						trigger: ['blur', 'change']
					},
					'wxksrq': {
						type: 'string',
						required: true,
						message: '不能为空',
						trigger: ['blur', 'change']
					},

				},
			};
		},
		onLoad(option) {
			this.form.scssid = option.scssid
		},
		onShow() {
			
		},
		methods: {
			async chooseFile() {
				let that = this
				uni.chooseImage({
					count: 1, //图片大于1可以多选,这样可以不超出最大上传值
					sourceType: ['album'],
					success: (res) => {
						this.list = []
						this.imageList = []
						console.log(res.tempFilePaths)
						res.tempFilePaths.forEach(item => {
							that.imageList.push(item)
						})
						that.imageList.forEach(item => {
							this.upImg(item)
						})
					}
				});
			},
			upImg(path) {
				uni.uploadFile({
					url: URL + `/common/upload`,
					filePath: path,
					header: {
						'Authorization': `Bearer ${uni.getStorageSync('token')}`
					},
					name: 'file',
					success: (res) => {
						console.log(JSON.parse(res.data).url)
						this.form.wxbzfj = JSON.parse(res.data).url
						this.list.push(res.url)
					}
				});
			},
			//删除图片
			delectImg(i) {
				uni.showModal({
					title: '提示',
					content: '确定要删除照片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imageList.splice(i, 1)
							this.list.splice(i, 1)

						}
						this.form.wxbzfj = null
					}
				})
			},
			//点击预览图片
			previewImg(current) {
				uni.previewImage({
					current,
					urls: this.imageList, //存放图片的数组
					loop: true,
					indicator: "default"
				})
			},

			// 选择日期
			confirm(val) {
				this.form.wxksrq = val[0].replace(new RegExp("-", "gm"), "");
				this.show = false
			},
			confirm1(val) {
				this.form.wxjsrq = val[0].replace(new RegExp("-", "gm"), "");
				this.show1 = false
			},
			// 维修状态
			confirm2(val) {
				this.form.wxzt = val.value[0].id
				this.form.wxztName = val.value[0].label
				this.show2 = false
			},
			submit() {

				this.$refs.form.validate().then(res => {
					uni.$u.toast('校验通过')
					wxadd(this.form).then(res => {
						uni.$u.toast('编辑成功')
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)

					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f5f5;
	}

	.container {
		padding: 0 24rpx;
		background-color: #f6f6f6;

		.title {
			font-size: 28rpx;
			font-weight: 500;
			line-height: 44rpx;
			padding: 20rpx 0;
		}

		.box {
			padding: 0rpx 48rpx;
			background-color: #fff;
			border-radius: 20rpx;
		}

		.textarea {
			
			.one {
				font-size: 28rpx;
				color: #999999;
			}

			.tarea {
				margin-top: 16rpx;
			}
		}
	}

	.but {
		margin: 30rpx 0;
	}
</style>
